package net.zjitc.yaobei_backed.service.sms.impl;

// 暂时注释掉腾讯云SDK导入，等依赖下载成功后再取消注释
// import com.tencentcloudapi.common.Credential;
// import com.tencentcloudapi.common.exception.TencentCloudSDKException;
// import com.tencentcloudapi.sms.v20210111.SmsClient;
// import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
// import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import net.zjitc.yaobei_backed.config.SmsConfig;
import net.zjitc.yaobei_backed.service.sms.SmsProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 腾讯云短信服务商实现
 * 支持个人用户注册，新用户有100条免费短信额度
 * 
 * 注意：暂时禁用，等腾讯云SDK依赖下载成功后再启用
 * 启用方法：取消下面的 @Component 注释，并取消导入语句的注释
 */
// @Component  // 暂时注释掉，避免编译错误
public class TencentSmsProvider implements SmsProvider {
    
    @Autowired
    private SmsConfig smsConfig;
    
    // 腾讯云短信服务地域，默认使用广州
    private static final String REGION = "ap-guangzhou";
    
    @Override
    public boolean sendVerificationCode(String phone, String code) throws Exception {
        SmsConfig.TencentConfig config = smsConfig.getTencent();
        
        // 验证配置
        if (config.getSecretId() == null || config.getSecretId().isEmpty() || 
            config.getSecretId().equals("your-tencent-secret-id")) {
            throw new IllegalArgumentException("腾讯云SecretId未配置，请在application.yml中配置sms.tencent.secret-id");
        }
        
        if (config.getSecretKey() == null || config.getSecretKey().isEmpty() || 
            config.getSecretKey().equals("your-tencent-secret-key")) {
            throw new IllegalArgumentException("腾讯云SecretKey未配置，请在application.yml中配置sms.tencent.secret-key");
        }
        
        if (config.getAppId() == null || config.getAppId().isEmpty() || 
            config.getAppId().equals("your-tencent-app-id")) {
            throw new IllegalArgumentException("腾讯云AppId未配置，请在application.yml中配置sms.tencent.app-id");
        }
        
        if (config.getSignName() == null || config.getSignName().isEmpty() || 
            config.getSignName().equals("your-sign-name")) {
            throw new IllegalArgumentException("腾讯云签名未配置，请在application.yml中配置sms.tencent.sign-name");
        }
        
        if (config.getTemplateId() == null || config.getTemplateId().isEmpty() || 
            config.getTemplateId().equals("your-tencent-template-id")) {
            throw new IllegalArgumentException("腾讯云模板ID未配置，请在application.yml中配置sms.tencent.template-id");
        }
        
        // 暂时返回未实现异常，等依赖下载成功后再取消注释下面的代码
        throw new UnsupportedOperationException("腾讯云短信功能暂时禁用，等SDK依赖下载成功后再启用。请先使用控制台模式（sms.enabled: false）");
        
        /* 等依赖下载成功后，取消注释下面的代码，并取消上面的throw语句
        try {
            // 实例化认证对象
            Credential cred = new Credential(config.getSecretId(), config.getSecretKey());
            
            // 实例化SMS客户端
            SmsClient client = new SmsClient(cred, REGION);
            
            // 实例化请求对象
            SendSmsRequest req = new SendSmsRequest();
            
            // 设置应用ID
            req.setSmsSdkAppId(config.getAppId());
            
            // 设置签名名称
            req.setSignName(config.getSignName());
            
            // 设置模板ID
            req.setTemplateId(config.getTemplateId());
            
            // 设置手机号（需要带国家码，如：+86）
            String phoneNumber = phone.startsWith("+") ? phone : "+86" + phone;
            req.setPhoneNumberSet(new String[]{phoneNumber});
            
            // 设置模板参数（验证码）
            req.setTemplateParamSet(new String[]{code});
            
            // 发送短信
            SendSmsResponse resp = client.SendSms(req);
            
            // 检查发送结果
            if (resp.getSendStatusSet() != null && resp.getSendStatusSet().length > 0) {
                String sendCode = resp.getSendStatusSet()[0].getCode();
                String sendMessage = resp.getSendStatusSet()[0].getMessage();
                
                if ("Ok".equals(sendCode)) {
                    return true;
                } else {
                    throw new Exception("腾讯云短信发送失败: " + sendMessage + " (Code: " + sendCode + ")");
                }
            } else {
                throw new Exception("腾讯云短信发送失败: 响应数据为空");
            }
            
        } catch (TencentCloudSDKException e) {
            throw new Exception("腾讯云短信发送异常: " + e.getMessage(), e);
        }
        */
    }
    
    @Override
    public String getProviderName() {
        return "腾讯云";
    }
}

